package com.zjj;


import com.zjj.pojo.Employee;
import com.zjj.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Scanner;

public class EmployeeMain {
    private static SqlSession session = MyBatisUtils.getSession();;

    public static void main(String[] args) {
        printMenu();
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();
        while (choice != 0 || choice < 0 || choice > 4) {
           if (choice == 1) {
                    System.out.println("==查询员工信息==");
                    System.out.println("0.返回上一级菜单");
                    System.out.println("1.查询所有员工信息");
                    System.out.println("2.查询指定员工信息");
                    System.out.println("请输入您的选择:");
                    int choice1 = sc.nextInt();
                    switch (choice1) {
                        case 0:
                            break;
                        case 1:
                            employeeFindAllTest();
                            break;
                        case 2:
                            employeeFindByIdTest();
                            break;
                        default:
                            System.out.println("输入错误，请重新输入");
                            break;
                    }
            }else if (choice == 2) {
                addEmployeeTest();
           }else if (choice == 3) {
               updateEmployeeTest();
           }else if (choice == 4) {
               deleteEmployeeTest();
           }else {
                    System.out.println("输入错误，请重新输入");
           }
           printMenu();
           choice = sc.nextInt();
        }
        System.out.println("欢迎下次光临");
        session.commit();
        session.close();
    }

    private static void printMenu(){
        System.out.println("欢迎使用员工管理系统");
        System.out.println("0.退出系统");
        System.out.println("1.查询员工信息");
        System.out.println("2.新增员工信息");
        System.out.println("3.修改员工信息");
        System.out.println("4.删除员工信息");
        System.out.println("请输入您的选择:");
    }

    /**
     * 查询指定员工信息
     */
    private static void employeeFindByIdTest(){

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要查询的员工id:");
        int id = sc.nextInt();
        Employee employee = session.selectOne("findEmployeeById",id);
        if(employee==null){
            System.out.println("查询的员工不存在");
            return;
        }else{
            System.out.println("查询的员工信息为:"+employee);
        }
    }

    /**
     * 查询所有员工信息
     */
    private static void employeeFindAllTest(){
        List<Employee> employee = session.selectList("findAllEmployee");
        for (Employee e : employee) {
            System.out.println(e.toString1());
        }

    }

    /**
     * 新增员工信息
     */
    private static void addEmployeeTest(){

        Scanner sc = new Scanner(System.in);
        Employee employee = new Employee();
        System.out.println("请输入员工姓名:");
        employee.setname(sc.next());
        System.out.println("请输入员工年龄:");
        int age = sc.nextInt();
        if(age<0 || age>100){
            System.out.println("输入错误，年龄不符合要求");
            return;
        }else {
            employee.setUage(age);
        }
        System.out.println("请输入员工职位:");
        String position = sc.next();
        if(position.equals("员工") || position.equals("经理")){
            employee.setPosition(position);
        }else {
             System.out.println("输入错误，职位只能是员工或经理");
             return;
        }

        int n = session.insert("addEmployee3",employee);
        if(n>0){
            System.out.println("新增员工成功,新增员工信息为:"+employee);
        }else{
            System.out.println("新增员工失败");
        }

    }
    /**
     * 修改员工信息
     */
    private static void updateEmployeeTest(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要修改的员工id:");
        int id = sc.nextInt();
        Employee employee = session.selectOne("findEmployeeById",id);
        if(employee==null){
            System.out.println("要修改的员工不存在");
            return;
        }
        System.out.println("请输入员工姓名:");
        employee.setname(sc.next());
        System.out.println("请输入员工年龄:");
        int age = sc.nextInt();
        if(age<0 || age>100){
            System.out.println("输入错误，年龄不符合要求");
            return;
        }else {
            employee.setUage(age);
        }

        System.out.println("请输入员工职位:");
        String position = sc.next();
        if(position.equals("员工") || position.equals("经理")){
            employee.setPosition(position);
        }else {
            System.out.println("输入错误，职位只能是员工或经理");
            return;
        }

        int n = session.update("updateEmployee",employee);
            if(n>0){
            System.out.println("修改员工成功,修改后的员工信息为:"+employee);
        }else{
            System.out.println("修改员工失败");
        }
    }
    /**
     * 删除员工信息
     */
    private static void deleteEmployeeTest(){

        Scanner sc = new Scanner(System.in);
        System.out.println("请输入要删除的员工id:");
        int id = sc.nextInt();
        Employee employee = session.selectOne("findEmployeeById",id);
        int n = session.delete("deleteEmployee",id);
        if(n>0){
            System.out.println("删除员工成功，员工信息为:"+employee);
        }else{
            System.out.println("删除员工失败");
        }
    }
}
